package com.example.supermarket.controller;

import com.example.supermarket.service.AdminHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>管理员模块控制器</p>
 * @author Bosen
 * @date 2021/10/8 17:24
 */
@RestController
public class AdminHomeController {

    @Autowired
    AdminHomeService service;

    /**
     * <p>管理员主页</p>
     */
    @RequestMapping({"/admin/index", "/admin"})
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("admin/index");
        return modelAndView;
    }

    /**
     * <p>管理员登录页面</p>
     */
    @RequestMapping({"/admin/login", "/"})
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("admin/login");
        return modelAndView;
    }

    /**
     * <p>退出登录</p>
     */
    @RequestMapping("/admin/logout")
    public ModelAndView logout(HttpServletRequest request, ModelAndView modelAndView) {
        modelAndView.setViewName("admin/login");
        service.logout(request);
        return modelAndView;
    }

    /**
     * <p>登录处理</p>
     * @return 1 登录成功 ; -1 登录失败
     */
    @RequestMapping("/admin/check/login/info")
    public int checkLoginInfo(HttpServletRequest request, String name, String password) {
        return service.checkLoginInfo(request, name, password) ? 1 : -1;
    }
}
